package jaxb;

import java.io.StringReader;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class Decodeur {
	
	private Unmarshaller  u = null;
	private static Decodeur instance = null;
	/*
	 * Architecture Singleton  qui permet de creer 1 seule fois l'objet Decodeur
	 */
	private Decodeur(){
		try {
			JAXBContext contexte = JAXBContext.newInstance("transfert.objects");
			u = contexte.createUnmarshaller();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static Decodeur getInstance(){
		if (instance == null)
		{
			instance = new Decodeur();
		}
		return (instance);
	}
	
	public Object decode(String Xml){
		StringBuffer xmlStr = new StringBuffer(Xml);
		Object objet = null;
		try {
			objet = u.unmarshal(new StreamSource(new StringReader(xmlStr.toString())));
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (objet);
		
	}
}
